package com.dongbao.util;

import com.dongbao.entity.AwardedInfo;
import com.dongbao.entity.CacheModule;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.serializers.BeanSerializer;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;

import java.util.ArrayList;

public class KryoSingleton{
    private KryoSingleton(){}
    public static Kryo getInstance(){
        return Singleton.INSTANCE.getInstance();
    }

    private static enum Singleton{
        INSTANCE;

        private Kryo singleton;
        //JVM会保证此方法绝对只调用一次
        private Singleton(){
            singleton = new Kryo();
            // 如果没有多层次的对象引用相互引用，设为false，提高性能
            singleton.setReferences(false);
            // 注册序列化/反序列化的类
            singleton.register(CacheModule.class);
//            singleton.register(AwardedInfo.class);

            CollectionSerializer serializer = new CollectionSerializer();
            serializer.setElementClass(AwardedInfo.class, new BeanSerializer<AwardedInfo>(singleton, AwardedInfo.class));
            serializer.setElementsCanBeNull(false);
            singleton.register(AwardedInfo.class, new BeanSerializer<AwardedInfo>(singleton, AwardedInfo.class));
            singleton.register(ArrayList.class, serializer);

        }
        public Kryo getInstance(){
            return singleton;
        }
    }
}
